package com.ufo.store.management.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.ufo.store.management.R;

/**
 *  作者：taoli on 2017/2/23 0023 15:26
 *  
 */
public class InputDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private OnItemClick mListener = null;
    private EditText mInputEt;
    private String mHint;

    public InputDialog(Context context) {
        super(context, R.style.select_dialog);
        this.context = context;
    }

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_input);
        mInputEt = (EditText) findViewById(R.id.input_et);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_ok).setOnClickListener(this);
        if (!TextUtils.isEmpty(mHint)) {
            mInputEt.setHint(mHint);
        }
    }

    public void setHint(String s) {
        mHint = s;
        if (mInputEt != null && !TextUtils.isEmpty(mHint)) {
            mInputEt.setHint(mHint);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                if(mListener!=null)
                    mListener.onItemClick(0,null);
                break;
            case R.id.tv_ok:
                if(mListener!=null) {
                    mListener.onItemClick(1, mInputEt.getText().toString());
                }
                break;
        }
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != InputDialog.this && InputDialog.this.isShowing()) {
                    InputDialog.this.dismiss();
                }
            }
        }, 100);
    }

    @Override
    public void setTitle(CharSequence title) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void show() {
        if (context != null && ((Activity) context).isFinishing()) {
            return;
        }
        super.show();
    }

    public void setItemClickListener(OnItemClick p) {
        mListener = p;
    }

    public interface OnItemClick {
        public void onItemClick(int position, String input);
    }
}